﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using AMS.DataAccess.Trademark.BO;
using System.Text;

public partial class RecvFile_ucRecvFileDetail : System.Web.UI.UserControl, IRecvFileEditorCtrl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        AddClientIDsToClient();
        if (!Utils.ParseIntQueryString(Request, "type", ref fileType))
        {
            fileType = FileTypeOffice;
        }

        if (!IsPostBack)
        {
            drpSource.Attributes["onchange"] = "javascript:onRFSourceChanged();";
            txtRFDate.Text = DateTime.Now.ToString("yyyy-MM-dd");
            drpSource.Enabled = false;
            ChangeUIByFileType();
        }
        else
        {
            LoadPostBackData();
        }

        AMSCKEditor.RegisterCKEditor(Page);
        ltrCKEditor.Text = AMSCKEditor.GetCreateCKEditorJs(txtContent.ClientID);
    }

    StringBuilder sbMsg;
    /// <summary>
    /// 默许为官方收文
    /// </summary>
    int fileType = FileTypeOffice;
    const int FileTypeOffice = 1;
    const int FileTypeCustomer = 2;

    void LoadPostBackData()
    {
        txtRFName.Text = Request[txtRFName.UniqueID];
        txtCustomer.Text = Request[txtCustomer.UniqueID];
        hfCustomerID.Value = Request[hfCustomerID.UniqueID];
        hfRFIndex.Value = Request[hfRFIndex.UniqueID];
        hfCaseID.Value = Request[hfCaseID.UniqueID];
        txtCaseIndex.Text = Request[txtCaseIndex.UniqueID];
    }

    void AddClientIDsToClient()
    {
        StringBuilder script = new StringBuilder();
        script.AppendFormat("var id_txtRFName = '{0}';\r\n", txtRFName.ClientID);
        script.AppendFormat("var id_hfRFIndex = '{0}';\r\n", hfRFIndex.ClientID);

        Page.ClientScript.RegisterStartupScript(Page.GetType(),
            "99C7983D-ADCD-49A1-97A2-757E9FF32729",
            script.ToString(),
            true);
    }

    void ChangeUIByFileType()
    {
        if (fileType == 1)
        {
            drpSource.SelectedIndex = 0;
            if (drpOffice.Items.Count > 0)
            {
                drpOffice.SelectedIndex = 0;
            }

            txtCustomer.Enabled = false;
        }
        else
        {
            drpSource.SelectedIndex = 1;
            drpOffice.Enabled = false;
        }
    }

    protected void drpOffice_DataBound(object sender, EventArgs e)
    {
        drpOffice.Items.Insert(0, new ListItem("", "0"));
    }

    public string ErrorMessage
    {
        get
        {
            if (sbMsg != null) { return sbMsg.ToString(); }
            else return string.Empty;
        }
    }

    public bool ValidateFormData()
    {
        // TODO: Add validations here
        bool bResult = true;
        if (sbMsg == null)
        {
            sbMsg = new StringBuilder();
        }

        if (string.IsNullOrEmpty(hfCustomerID.Value) || hfCustomerID.Value == "0")
        {
            sbMsg.Append("请选择或新建客户; ");
            bResult = false;
        }
        if (string.IsNullOrEmpty(txtRFDate.Text))
        {
            sbMsg.Append("请选择收文日期; ");
            bResult = false;
        }

        return bResult;
    }

    /// <summary>
    /// 获取收文信息
    /// </summary>
    /// <param name="rf"></param>
    /// <returns></returns>
    public bool GetRecvFileInfo(RecvFileInfo rf)
    {
        if (!ValidateFormData())
        {
            return false;
        }

        rf.RFName = txtRFName.Text;
        rf.CaseID = int.Parse(hfCaseID.Value);
        rf.CustomerID = int.Parse(hfCustomerID.Value);
        rf.RFContent = txtContent.Text;
        rf.RFIndex = hfRFIndex.Value;
        rf.RFName = txtRFName.Text;
        rf.Source = drpSource.SelectedValue;
        if (!string.IsNullOrEmpty(txtSentFileDate.Text))
        {
            rf.FileSentDate = DateTime.Parse(txtSentFileDate.Text);
        }
        if (!string.IsNullOrEmpty(txtAnswerDate.Text))
        {
            rf.AnswerDate = Convert.ToDateTime(txtAnswerDate.Text);
        }
        if (!string.IsNullOrEmpty(txtRFDate.Text))
        {
            rf.RecvDate = Convert.ToDateTime(txtRFDate.Text);
        }
        if (drpOffice.Enabled && drpOffice.SelectedIndex > 0)
        {
            rf.OfficeID = int.Parse(drpOffice.SelectedValue);
        }

        return true;
    }

    public void EditRecvFile(RecvFileInfo rf)
    {
        txtAnswerDate.Text = Utils.DateTimeToString(rf.AnswerDate);
        txtRFDate.Text = Utils.DateTimeToString(rf.RecvDate);
        txtSentFileDate.Text = Utils.DateTimeToString(rf.FileSentDate);
        txtContent.Text = rf.RFContent;
        txtCustomer.Text = rf.CustomerName;
        txtCaseIndex.Text = rf.CaseIndex;
        txtRFName.Text = rf.RFName;
        hfCustomerID.Value = rf.CustomerID.ToString();
        hfCaseID.Value = rf.CaseID.ToString();
        hfRFIndex.Value = rf.RFIndex;
        if (rf.OfficeID != 0)
        {
            drpOffice.SelectedValue = rf.OfficeID.ToString();
        }
        if (!string.IsNullOrEmpty(rf.Source))
        {
            drpSource.SelectedValue = rf.Source;
        }

        drpSource.Enabled = false;
        if (rf.Source == "O")
        {
            fileType = FileTypeOffice;
        }
        else
        {
            fileType = FileTypeCustomer;
        }

        ChangeUIByFileType();
        //txtCustomer.Enabled = false;
        //txtCaseIndex.Enabled = false;
    }
}
